package com.motus.pm.action;

import java.sql.Timestamp;
import java.util.*;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.motus.pm.service.ProjectService;
import com.motus.pm.vo.PmPhaseInfo;
import com.motus.pm.vo.PmProjectInfo;
import com.motus.pm.vo.PmProjectPhase;
import com.motus.pm.vo.PmProjectPhaseId;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
public class ProjectAction extends ActionSupport implements
		ModelDriven<PmProjectInfo> {

	private static final long serialVersionUID = 1L;
	// 新建项目功能所需参数
	private Timestamp[] phasePlanStartTimes;
	private Timestamp[] phasePlanEndTimes;
	private Integer[] phasePoints;
	private String[] remarks;

	@Autowired
	@Qualifier("projectServiceImpl")
	private ProjectService projectService;

	@Autowired
	private PmProjectInfo pmProjectInfo;

	private List<PmProjectInfo> projectInfos;

	private List<PmProjectPhase> projectPhases;

	public Timestamp[] getPhasePlanStartTimes() {
		return phasePlanStartTimes;
	}

	public void setPhasePlanStartTimes(Timestamp[] phasePlanStartTimes) {
		this.phasePlanStartTimes = phasePlanStartTimes;
	}

	public Timestamp[] getPhasePlanEndTimes() {
		return phasePlanEndTimes;
	}

	public void setPhasePlanEndTimes(Timestamp[] phasePlanEndTimes) {
		this.phasePlanEndTimes = phasePlanEndTimes;
	}

	public Integer[] getPhasePoints() {
		return phasePoints;
	}

	public void setPhasePoints(Integer[] phasePoints) {
		this.phasePoints = phasePoints;
	}

	public String[] getRemarks() {
		return remarks;
	}

	public void setRemarks(String[] remarks) {
		this.remarks = remarks;
	}

	public List<PmProjectPhase> getProjectPhases() {
		return projectPhases;
	}

	public void setProjectPhases(List<PmProjectPhase> projectPhases) {
		this.projectPhases = projectPhases;
	}

	public List<PmProjectInfo> getProjectInfos() {
		return projectInfos;
	}

	public void setProjectInfos(List<PmProjectInfo> projectInfos) {
		this.projectInfos = projectInfos;
	}

	@Override
	public PmProjectInfo getModel() {
		return pmProjectInfo;
	}

	// 默认为进入项目列表展示
	@Override
	public String execute() throws Exception {
		// List<PmProjectInfo> projectInfos =
		// projectService.getAllProjectInfos();
		// 获取到所有状态由-1到1的项目
		List<PmProjectInfo> projectInfos = projectService
				.getProjectInfosByState(-1);
		projectInfos.addAll(projectService.getProjectInfosByState(0));
		projectInfos.addAll(projectService.getProjectInfosByState(1));
		ServletActionContext.getRequest().setAttribute("projectInfos",
				projectInfos);
		return super.execute();
	}

	// 项目详情
	public String projectDetail() {
		String pid = pmProjectInfo.getPid();
		projectPhases = projectService.getProjectPhasesByPid(pid);
		return "success";
	}

	// 新建项目界面
	public String newProject() {
		// List<PmPhaseInfo> phaseInfos = projectService.getAllPhaseInfos();
		// ServletActionContext.getRequest()
		// .setAttribute("phaseInfos", phaseInfos);
		projectInfos = projectService.getProjectInfosByState(-2);
		return "success";
	}

	// 执行新建项目动作
	public String newProjectExec() {
		// 因为前台只能获取pid字段，所以需要 重新查询
		String pid = pmProjectInfo.getPid();
		PmProjectInfo projectInfo = projectService.getProjectInfoById(pid);
		pmProjectInfo.setPname(projectInfo.getPname());
		pmProjectInfo.setProPoint(projectInfo.getProPoint());
		// 获取所有阶段名称
		List<PmPhaseInfo> phaseInfos = projectService.getAllPhaseInfos();
		// 处理前台时间数组封装成为ProjectPhases集合
		Set<PmProjectPhase> pmProjectPhases = new HashSet<>();
		for (int i = 0; i < phasePlanStartTimes.length; i++) {
			// 如果某一阶段的起始时间均未给出，此阶段不计入项目阶段安排
			if (phasePlanStartTimes[i] == null && phasePlanEndTimes[i] == null) {
				continue;
			}
			// 新建PmProjectPhase对象
			PmProjectPhase pmProjectPhase = new PmProjectPhase();
			PmProjectPhaseId pmProjectPhaseId = new PmProjectPhaseId();

			// 获取要保存的值
			String phaseId = phaseInfos.get(i).getPhaseId();
			Timestamp phasePlanStartTime = phasePlanStartTimes[i];
			Timestamp phasePlanEndTime = phasePlanEndTimes[i];
			int phasePoint = phasePoints[i];
			String remark = remarks[i];

			// 根据phaseId值获取对应的PmPhaseInfo对象
			PmPhaseInfo pmPhaseInfo = projectService.getPmPhaseById(phaseId);

			// 设置PmProjectPhase的主键
			pmProjectPhaseId.setPmProjectInfo(pmProjectInfo);
			pmProjectPhaseId.setPmPhaseInfo(pmPhaseInfo);

			// 设置PmProjectPhase属性
			pmProjectPhase.setId(pmProjectPhaseId);
			pmProjectPhase.setPhasePlanStartTime(phasePlanStartTime);
			pmProjectPhase.setPhasePlanEndTime(phasePlanEndTime);
			pmProjectPhase.setPhasePoint(phasePoint);
			pmProjectPhase.setRemark(remark);
			pmProjectPhase.setPhaseState(-1);
			pmProjectPhases.add(pmProjectPhase);
		}
		try {
			pmProjectInfo.setPmProjectPhases(pmProjectPhases);
			// -1代表项目已立项，初始值为-2
			pmProjectInfo.setProState(-1);
			projectService.updateProject(pmProjectInfo);
		} catch (Exception e) {
			return "fail";
		}
		return "success";
	}

	// 修改项目
	public String updateProject() {
		String pid = pmProjectInfo.getPid();
		projectPhases = projectService.getProjectPhasesByPid(pid);
		return "success";
	}

	// 执行修改项目动作
	public String updateProjectExec() {
		String pid = pmProjectInfo.getPid();
		// 获取前台未传递的项目分数值
		PmProjectInfo projectInfo = projectService.getProjectInfoById(pid);
		pmProjectInfo.setProPoint(projectInfo.getProPoint());
		projectPhases = projectService.getProjectPhasesByPid(pid);
		// 处理前台时间数组封装成为ProjectPhases集合
		Set<PmProjectPhase> pmProjectPhases = new HashSet<>();
		for (int i = 0; i < phasePlanStartTimes.length; i++) {
			// 如果某一阶段的起始时间均未给出，此阶段不计入项目阶段安排
			if (phasePlanStartTimes[i] == null && phasePlanEndTimes[i] == null) {
				continue;
			}
			// 新建PmProjectPhase对象
			PmProjectPhase pmProjectPhase = new PmProjectPhase();
			PmProjectPhaseId pmProjectPhaseId = new PmProjectPhaseId();

			// 获取要保存的值
			String phaseId = projectPhases.get(i).getId().getPmPhaseInfo()
					.getPhaseId();
			Timestamp phasePlanStartTime = phasePlanStartTimes[i];
			Timestamp phasePlanEndTime = phasePlanEndTimes[i];
			int phasePoint = phasePoints[i];
			String remark = remarks[i];

			// 根据phaseId值获取对应的PmPhaseInfo对象
			PmPhaseInfo pmPhaseInfo = projectService.getPmPhaseById(phaseId);

			// 设置PmProjectPhase的主键
			pmProjectPhaseId.setPmProjectInfo(pmProjectInfo);
			pmProjectPhaseId.setPmPhaseInfo(pmPhaseInfo);

			// 设置PmProjectPhase属性
			pmProjectPhase.setId(pmProjectPhaseId);
			pmProjectPhase.setPhasePlanStartTime(phasePlanStartTime);
			pmProjectPhase.setPhasePlanEndTime(phasePlanEndTime);
			pmProjectPhase.setPhasePoint(phasePoint);
			pmProjectPhase.setRemark(remark);
			// 此处设置为-1是指该阶段尚未开始，才允许进入修改界面，所以能进入修改项目界面，一定是阶段尚未开始
			pmProjectPhase.setPhaseState(-1);
			pmProjectPhases.add(pmProjectPhase);
		}
		try {
			pmProjectInfo.setPmProjectPhases(pmProjectPhases);
			pmProjectInfo.setProState(-1);
			projectService.updateProject(pmProjectInfo);
		} catch (Exception e) {
			return "fail";
		}
		return "success";
	}

	// 删除项目
	public String deleteProject() {
		String pid = pmProjectInfo.getPid();
		projectService.deleteProject(projectService.getProjectInfoById(pid));
		return "success";
	}

}
